package lc.q51_100;

public class Q59 {

    public int[][] generateMatrix(int n) {
        int[][] nums = new int[n][n];
        // 先画第一层，再画第二层
        int k = 1;
        for (int i = 0; i * 2 < n; i++) {
            for (int j = i; j < n - i && k <= n*n; j++) {
                nums[i][j] = k;
                k ++;
            }
            for (int j = i+1; j < n-i && k <= n*n; j++) {
                nums[j][n-i-1] = k;
                k ++;
            }
            for (int j = n - i - 2; j >= i && k <= n*n; j--) {
                nums[n-i-1][j] = k;
                k ++;
            }
            for (int j = n - i - 2; j > i && k <= n*n; j--) {
                nums[j][i] = k;
                k ++;
            }
        }
        return nums;
    }

    public static void main(String[] args) {
        Q59 q59 = new Q59();
        int[][] ints = q59.generateMatrix(0);
        for (int[] anInt : ints) {
            for (int i : anInt) {
                System.out.print(i +",");
            }
            System.out.println();
        }
    }

}
